<?php
class Rebate extends CI_Controller {
	function __construct() {
		parent::__construct ();
		session_start();
		if (! isset ( $_SESSION ["admin_id"] ) || $_SESSION ["admin_id"] == "") {
			header ( "Location:" . site_url ( 'admin/login' ) );
		}
		$this->load->model ( 'm_rebate', 'reb', TRUE );
		$this->load->helper ( 'url' );
	}
	function index($page = 0) {
		// 获取所有申请信息
		$data['limit'] = 10;
		$data['page'] = $page;
		$lang ['list'] = $this->reb->tot_list($data);
		$lang['prepage'] = ($page-1<0)?0:$page-1;
		$lang['nextpage'] = $page+1;
		$this->load->view ( 'admin/v_rebate', $lang );
	}
	
	function need_reb(){
		// 获取检测通过需返利的申请信息
		$lang ['list'] = $this->reb->_list ();
		$this->load->view ( 'admin/v_rebate', $lang );
	}
	// 对需返利表进行更新
	function check_can() {
		// 检测非会员用户提交到rebate表的申请是否有效
		$lang ['list'] = $this->reb->check_can ();
		$this->load->view ( 'admin/v_rebate', $lang );
	}
	
	function user_can() {
		// 检测会员账号提交到rebate表的申请是否有效
		$lang ['list'] = $this->reb->user_can ();
		$this->load->view ( 'admin/v_rebate', $lang );
	}
	
	function rebate_tot() {
		// 在获取到报表后对会员账户中的fanli字段进行更新。（可在此插入邮件提醒）
		$r = $this->reb->_tot ();
		$fail = 0;
		foreach ($r as $v){
			$w["id"] = $v["id"];
			$rs = $this->m_tbk->getOne("user",$w);
			if (preg_match ( "/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/", $rs["usermail"] )){
				if(!$this->sendRemind($rs["usermail"],$rs["fanli"])){
					$fail++;
				}
			}else{
				$w1["user_id"] = $v["id"];
				$h = $this->getOne("union_users",$w1);
				if(!$this->sendRemind($h["email"],$rs["fanli"])){
					$fail++;
				}
			}
		}
		if ($r) {
			$res["sendmail"] = $fail;
			$res["state"] = 1;
		} else {
			$res["state"] = 0;
		}
		echo json_encode ( $res );
	}
	//发送邮件提醒
	private function sendRemind($usermail,$fanli) {
		$_to = $usermail;
		$config = Array (
				'protocol' => 'smtp',
				'smtp_host' => 'smtp.163.com', // 服务器
				'smtp_port' => 25,
				'smtp_user' => "geili0574",
				'smtp_pass' => '0m6YDR0y', // password
				'mailtype' => 'html', // 邮件类型
				'charset' => 'utf-8'  // 编码
		);
		$this->load->library ( 'email', $config );
		$this->email->set_newline ( "\r\n" );
	
		$from_name = "给力网"; // 发件人名称
		$email_subject = "返利提醒"; // 标题
	
		$url = site_url ( "public/getpass/getpassresetpwd" );
		$email_body = "
		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
		<tbody>
		<tr>
		<td>
		<table cellpadding=0 cellspacing=0 width=100%>
		<tbody>
		<tr>
		<td width=25px; style=width:25px;></td>
		<td align=>
		<div style=line-height:40px;height:40px;></div>
		<p style=margin:0px;padding:0px;><strong style=font-size:14px;line-height:24px;color:#333333;font-family:arial,sans-serif;>亲爱的用户：</strong></p>
		<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>您好！</p>
		<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>您在" . date ( "Y年m月d日 H:i:s", time () ) . "获取的了从给力网的返利</p>
		<p style=margin:0px;padding:0px;><a href=" . $url . " style=line-height:24px;font-size:12px;font-family:arial,sans-serif;color:#0000cc></a></p>
		<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#979797;font-family:arial,sans-serif;>您现在，在给力网的可提取金额为".$fanli."</p>
		<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>请及时等www.geili0574.com 查看！谢谢</p>
		<div style=line-height:80px;height:80px;></div>
		<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>TB返利中心</p>
		<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>" . date ( "Y年m月d日", time () ) . "</p>
		</td>
		</tr>
		</tbody>
		</table>
		</td>
		</tr>
		</tbody>
		</table>
		</body>
		</html>";
	
		// 解决手机提醒乱码问题
		$this->email->from ( "geili0574@163.com", $from_name ); // 发件人
		$this->email->to ( $_to ); // 收件人
	
		$this->email->subject ( $email_subject );
		$this->email->message ( $email_body );
	
		if (! $this->email->send ()) {
			return false;
		} else {
			return  true;
		}
	}
	
	function top() {
		$this->load->view ( 'admin/v_top' );
	}
	function left() {
		$this->load->view ( 'admin/v_left' );
	}
	function right() {
		$this->load->view ( 'admin/v_right' );
	}
	function right_top() {
		$this->load->view ( 'admin/v_right_top' );
	}
	
	// 对单个返利申请进行完成操作
	function done() {
		$id = $this->input->post ( 'id' );
		$val = $this->reb->_done ( $id );
		if ($val) {
			$res = $this->_lang ['act_success'];
		} else {
			$res = $this->_lang ['act_failed'];
		}
		echo json_encode ( $res );
		exit ();
	
	}
}